package com.service.xbq;

import com.dao.BaseDao;
import com.entity.CatPageManager;
import com.entity.CatPost;
import com.entity.PageManager;
import com.entity.User;
import com.utils.EncryptCodeUtils;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.List;

public class HeadService extends BaseDao {
    //查询所有
    public List<User> queryUsers() {
        String sql = "select * from user";
        return findList(User.class, sql);
    }



    public int update(Object obj) throws Exception {

        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "UPDATE " + tableName +" SET ";
        //参数部分
        String sqlParams = "";
        //循环所有的字段
        Field[] fields = clas.getDeclaredFields();
        //参数
        Object[] params = new Object[fields.length];
        int i = 0;
        for (Field field : fields) {
            String name = field.getName();
            if (name.equals("id"))
                continue;
            sqlParams += name+"=?,";
            //调用get方法
            //获取方法
            String getMethodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
            params[i] = clas.getDeclaredMethod(getMethodName).invoke(obj);
            i++;
        }
        //去掉最后一个逗号
        sqlParams = sqlParams.substring(0, sqlParams.length()-1);

        sql = sql + sqlParams + " where id = ?";System.out.println(sql);
        //最后一个位置放入id
        params[params.length-1] = clas.getDeclaredMethod("getId").invoke(obj);

        return update(sql, params);
    }

//根据用户名查询
    public User queryUserByUsername(String username) {
        String sql = "select * from user where username = ?";
        return findBean(User.class, sql, username);
    }



    public int updateMessage(int id,String name,String telephone,String qq,String wx,String Email,String pic){
        User user = new HeadService().queryUser(id);
        String sql="UPDATE `rz1217db_2`.`user` SET `name` = ?, `telephone` = ?, `qq` = ?, `wx` = ?, `Email` = ?,`pic` = ? WHERE `id` = ?";
     if (pic ==null ){
         pic ="a";
     }
        Object params[] = {name,telephone,qq,wx,Email,pic,id};
        for (int i = 0; i < params.length; i++) {
            System.out.println(params[i]);
        }
           int result = update(sql, params);
           if (result >0 && user.getPic() != null){
               new File(user.getPic()).delete();
           }
           return result;
    }

    public User queryUser(int id) {
//        String sql = "select * from user where id = ?";
        try {
            return (User) query(new User(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int password(String password,User user){
        String password1 = EncryptCodeUtils.getEncryptStr(password,
                user.getPhone());
        String password2 =  querypassword(user.getId()).getPassword();
        if (password1.equals(password2)){
            return 1;
        }
        return 0;
    }
//查询用户密码
    public User querypassword(int id) {
        String sql = "select * from user where id = ?";
        return findBean(User.class, sql, id);
    }
    //更改用户密码
    public int updatePassword(int id,String password) {
      User user =   queryUser(id);
        String pas = EncryptCodeUtils.getEncryptStr(password,
                user.getPhone());
        String sql = "UPDATE `rz1217db_2`.`user` SET `passWord` = ? WHERE `id` = ?";
        Object parms[] = {pas,id};
       return update(sql,parms);
    }

    public int counts(String sql,String username) {
        try {
            Long l = runner.query(sql, new ScalarHandler<>(),username);
            return Integer.parseInt(l.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //分页查询用户发帖
    public List<CatPost> queryCatPost(CatPageManager pageManager, String username){
        int start = (pageManager.getPn() - 1) * pageManager.getRn();
        int end = pageManager.getRn();
        String sql = "select * from catpost where username = ? limit ?,?";
        return findList(CatPost.class, sql, username,start, end);
    }

    public int queryUserTotalCount(String username) {
        String sql = "select count(*) from catpost where username = ?";
        return counts(sql,username);
    }
//查询用户审核总数
public int queryUserTotalCount2(String username) {
    String sql = "select count(*) from catpost where username = ? and state = 'F' ";
    return counts(sql,username);
}


    //删除帖子
    public  int delete(int id){
        String sql = "DELETE FROM `rz1217db_2`.`catpost` WHERE `id` = ?";
        int update = update(sql, id);
        return update;
    }

    //查询帖子
    public CatPost queryCatpost(int postid){
        String sql = "select * from catpost where id =?";
        return findBean(CatPost.class,sql,postid);
    }

    //查询审核中得帖子帖子
    public  List<CatPost>  queryCatPost2(CatPageManager pageManager, String username){
        int start = (pageManager.getPn() - 1) * pageManager.getRn();
        int end = pageManager.getRn();
        String sql = "select * from catpost where username = ? and state = 'F' limit ?,?";
        return  findList(CatPost.class, sql, username,start, end);
    }

    //查询用户发帖量
    public int queryUserpost(String username){
        String sql = "select count(*) from catpost where username = ?  ";
        return counts(sql,username);
    }
//充值添加帖子数量
public int chongzhi(int  number,String username){
    int count =   queryNumber(username);
    //  System.out.println(count);
    count =count+number;
    String sql = "UPDATE `rz1217db_2`.`user` SET `number` = ? WHERE `username` = ?";
    return  update(sql,count,username);

}

    //查询用户剩余发帖量
    public  int queryNumber(String username){
        String sql = "select number from user where username = ?";
        int count = count2(sql,username);
        return count;
    }

    public int count2(String sql,String catpost) {
        try {
            Integer l = runner.query(sql, new ScalarHandler<>(),catpost);
            return l;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
